package com.boco.pciv;

import com.boco.pciv.core.entity.Constants;
import com.boco.pciv.core.security.LoginUser;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.net.URL;
/**
 * Created by CC on 2017/6/6.
 * 网站测试配置
 */
@AutoConfigureMockMvc
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT)
public abstract class WebTestBase {
    @LocalServerPort
    private int port;
    protected URL base;

    @Before
    public void setUp() throws Exception {
        this.base = new URL("http://localhost:"+port+"/");

        System.out.println("url:"+this.base.toString());
    }

    protected String formatUrl(String url){
        return String.format("%s%s",this.base,url);
    }

protected MockHttpSession getSession(){
    //        //模拟登录用户
    MockHttpSession session = new MockHttpSession();
    LoginUser user = getTestUser();
        session.setAttribute(Constants.CurrentUser,user);
        session.setAttribute(Constants.CurrentUserId,user);

    return session;
}
    protected LoginUser getTestUser(){
        LoginUser user = new LoginUser();
        user.setUserId("testuid");
        user.setUserName("testUsername");
        return user;
    }
}
